//
//  JHWelcomeViewController.swift
//  JHSinaWeibo
//
//  Created by apple on 16/5/30.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit


class JHWelcomeViewController: UIViewController {

    override func loadView() {
        view = backView
    }
    
    override func viewDidLoad() {
        setUpUI()
    }
    //设置ui界面
    private  func setUpUI(){
    
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        //设置约束
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //设置头像框中的图片
        iconView.sd_setImageWithURL(JHUserAccountModel.sharedAccountViewModel.headURL)
        iconView.cornerRadius = 45
        //设置nameLabel的透明度
     nameLabel.alpha =  0
    }
    //动画效果建议在viewDidAppear阶段执行
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        starAnimation()
    }
    private func starAnimation(){
        //弹簧动画效果
        //usingSpringWithDamping: 阻尼系数 0 ~ 1 值越小 效果越弹
        //initialSpringVelocity : 加速度 值越大 速度越快
        //OC:位移枚举 表示多个动画选项 '|' swift: 使用数组数组
        let offset = 160 - ScreenHeight
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: { 
            //跟新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            
            //提前刷新父类视图
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLabel.alpha = 1
                    }, completion: { (_) in
                        print("ok")
                })
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
        }
    
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    //懒加载子视图
    //背景视图
   private lazy var backView:UIImageView = UIImageView(image:UIImage(named:"ad_background"))
    //用户图像
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    //用户名称文案
    private lazy var nameLabel:UILabel  = UILabel(title: JHUserAccountModel.sharedAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
    
//    private lazy var nameLabel: UILabel = {
//    let l = UILabel()
//        l.text = JHUserAccountModel.sharedAccountViewModel.userAccount?.name
//        l.font = UIFont.systemFontOfSize(18)
//        l.textColor = UIColor.darkGrayColor()
//        l.alpha = 0
//        //自适应大小
//        l.sizeToFit()
//    return l
//    }()
    
}
